package com.zcsy.manage.web.property;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.unique.UniqueNumberUtils;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.property.RepairReply;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.property.IRepairReplyService;
import com.zcsy.manage.web.common.LogUtils;

/**
 * @Title: RepairReplyController.java
 * @Package： com.zcsy.manage.web.property
 * @Description: 报修回复控制层管理类
 * @author xujl
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年4月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/repairReply")
public class RepairReplyController {

    @Autowired
    private IRepairReplyService repairReplyService;

    /**
     * @Description 编辑报修回复<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "editreplyInfo", method = RequestMethod.GET)
    public String editrepairInfo(Model model, String id) {
        try {

            model.addAttribute("type", Constants.SAVE_TYPE);

        } catch (Exception e) {
        }
        return "property/repairreply/replyInfoEdit";
    }

    /**
     * @Description 物业保存报修回复
     * @param title
     *            标题
     * @param content
     *            内容
     * @param type
     *            回复类型1：物业，2：业主
     * @param memo
     *            备注
     * @param request
     *            用户的请求
     * @param response
     *            返回的响应
     * @return 返回保存的结果
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "saveReply", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveReply(String repairId, String title, String content, Integer type,
                                 String memo, HttpServletRequest request,
                                 HttpServletResponse response) {
        RequestJson result = new RequestJson();
        User user = (User)request.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        try {
            RepairReply record = new RepairReply();
            record.setTitle(title);
            record.setContent(content);
            record.setMemo(memo);
            record.setReplyType(type);
            record.setId(UniqueNumberUtils.get18UniqueNumber());
            record.setReplyTime(DateUtils.getTimestamp());
            record.setReplyAccount(user.getUsername());
            record.setRepairId(repairId);

            repairReplyService.insert(record);
            LogUtils.insertLog("用户回复报修信息", user.getId(), request);
        } catch (Exception e) {
            result.setSuccess(false);
        }
        return result;
    }

}
